<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" type="text/css" href="../../static/css-tuangou/bootstrap.min.css"/>
  <link rel="stylesheet" type="text/css" href="../../static/css-tuangou/buttons.css"/>
  <link rel="stylesheet" type="text/css" href="../../static/css-tuangou/alert.css"/>
  <link rel="stylesheet" type="text/css" href="../../static/css-tuangou/cart.css"/>
  <script src="../../static/js-tuangou/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
  <script src="../../static/js-tuangou/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
  <title>查询用户信息</title>
  <!--  Layui  -->
  <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.6.8/css/layui.css">
  <script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container">

    <div class="navbar-header">
      <a href="le-index.html" class="navbar-brand">
        <span class="glyphicon glyphicon-home le-text"></span>
        <span class="le-text">乐购商城</span>
      </a>

      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#le-nav">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>

    </div>

    <div class="navbar-collapse collapse" id="le-nav">
      <ul class="navbar-nav nav hidden-xs">
        <li><a href="">乐购 惊喜 盛大开业 欢迎来购</a></li>
      </ul>

      <ul class="navbar-nav nav navbar-right">
        <li><a href="le-index.html">关于乐购</a></li>
        <li><a href="le-mygroupbying.html">我的拼团</a></li>
        <li><a href="le-cart.html">购物车</a></li>
        <li><a href="le-balance.html">充值</a></li>
        <li><a href="le-list.html">用户信息</a></li>


        <!-- 当没有登录时 点击会跳转到登录页面 -->

        <!-- 下拉列表阻止a超链接的默认行为 -->
        <li class="dropdown">
          <a data-toggle="modal" data-target="#loginModal" class="dropdown-toggle" id="le-login">登录</a>
          <!-- 下拉菜单 -->
          <ul class="dropdown-menu">
            <!--                        <li><a href="le-personal.html">个人中心</a></li>-->
            <!--                        <li><a href="" id="update-pwd" data-toggle="modal" data-target="#bt-modal2">修改密码</a></li>-->
            <li><a href="" id="le-user-logout" data-toggle="modal" data-target="#bt-modal">退出</a></li>
          </ul>
        </li>
        <li><a data-toggle="modal" data-target="#registerModal">注册</a></li>
      </ul>
    </div>
  </div>
</nav>
<div class="le-top-ad">


</div>

<div class="layui-container">
  <div class="layui-row">
    <div class="layui-col-md6 layui-col-md-offset3">
      <div class="layui-card">
        <div class="layui-card-header">查询用户信息</div>
        <div class="layui-card-body">

          <div class="layui-form layui-form-pane">
            <div class="layui-form-item">
              <label class="layui-form-label">用户ID：</label>
              <div class="layui-input-block">
                <input type="text" id="userId" lay-verify="required" autocomplete="off" class="layui-input">
              </div>
            </div>

            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="search"style="background-color: #b21f2d">查询</button>
                <button class="layui-btn layui-btn-primary" onclick="returnToIndex()">返回首页</button>
              </div>
            </div>

            <div id="userInfo" class="layui-form-item layui-hide">
              <div class="layui-input-block">
                <p>ID: <span id="userIdValue"></span></p>
                <p>用户名: <span id="username"></span></p>
                <p>电话: <span id="phone"></span></p>
                <p>邮箱: <span id="email"></span></p>
                <p>余额: <span id="balance"></span></p>
              </div>
            </div>
          </div>

          <div class="layui-form-item">
            <div class="layui-input-inline" style="float: left;">
              <input type="text" id="newEmail" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="float: left;">
              <button class="layui-btn" id="updateEmailBtn"style="background-color: #b21f2d">更新Email</button>
            </div>
          </div>

          <div class="layui-form-item">
            <div class="layui-input-inline" style="float: left;">
              <input type="text" id="newUsername" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="float: left;">
              <button class="layui-btn" id="updateUsernameBtn" style="background-color: #b21f2d">更新名称</button>
            </div>
          </div>

          <div class="layui-form-item">
            <div class="layui-input-inline" style="float: left;">
              <input type="text" id="newPhone" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline" style="float: left;">
              <button class="layui-btn" id="updatePhoneBtn"style="background-color: #b21f2d">更新电话</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<!--  Layui script -->
<script src="https://cdn.staticfile.org/layui/2.6.8/layui.js"></script>

<script>
  layui.use(['form', 'jquery'], function () {
    var form = layui.form;
    var $ = layui.jquery;

    form.on('submit(search)', function (data) {
      var userId = $('#userId').val();
      if (userId.trim() !== '') {
        $.ajax({
          type: 'GET',
          url: '/tbUser/' + userId,
          success: function (response) {
            $('#userIdValue').text(response.data.uid);
            $('#username').text(response.data.username);
            $('#phone').text(response.data.phone);
            $('#email').text(response.data.email);
            $('#balance').text(response.data.balance);
            $('#userInfo').removeClass('layui-hide');
          },
          error: function (xhr, status, error) {
            console.error(xhr.responseText);
            $('#userInfo').addClass('layui-hide');
            layer.msg('查询失败，请重试！', {icon: 5});
          }
        });
      } else {
        layer.msg('请输入用户ID！', {icon: 5});
      }
      return false; lknw
    });

    $('#updateEmailBtn').click(function () {
      var userId = $('#userId').val();
      var newEmail = $('#newEmail').val();
      if (userId.trim() !== '' && newEmail.trim() !== '') {
        $.ajax({
          type: 'POST',
          url: '/tbUser/updateEmail',
          contentType: 'application/json',
          data: JSON.stringify({uid: userId, email: newEmail}),
          success: function (response) {
            layer.msg('Email更新成功！', {icon: 1});
          },
          error: function (xhr, status, error) {
            console.error(xhr.responseText);
            layer.msg('Email更新失败，请重试！', {icon: 5});
          }
        });
      } else {
        layer.msg('请输入用户ID和新Email！', {icon: 5});
      }
    });
  });
  $('#updateUsernameBtn').click(function () {
    var userId = $('#userId').val();
    var newUsername = $('#newUsername').val();
    if (userId.trim() !== '' && newUsername.trim() !== '') {
      $.ajax({
        type: 'POST',
        url: '/tbUser/updateUsername',
        contentType: 'application/json',
        data: JSON.stringify({uid: userId, username: newUsername}),
        success: function (response) {
          layer.msg('用户名更新成功！', {icon: 1});
          $('#currentUsername').text(newUsername);
        },
        error: function (xhr, status, error) {
          console.error(xhr.responseText);
          layer.msg('用户名更新失败，请重试！', {icon: 5});
        }
      });
    } else {
      layer.msg('请输入用户ID和新用户名！', {icon: 5});
    }
  });
  $('#updatePhoneBtn').click(function () {
    var userId = $('#userId').val();
    var newPhone = $('#newPhone').val();
    if (userId.trim() !== '' && newPhone.trim() !== '') {
      $.ajax({
        type: 'POST',
        url: '/tbUser/updatePhone',
        contentType: 'application/json',
        data: JSON.stringify({uid: userId, phone: newPhone}),
        success: function (response) {
          layer.msg('电话号码修改成功！', {icon: 1});
          $('#currentPhone').text(newPhone);
        },
        error: function (xhr, status, error) {
          console.error(xhr.responseText);
          layer.msg('电话号码修改失败，请重试！', {icon: 5});
        }
      });
    } else {
      layer.msg('请输入用户ID和新电话号码！', {icon: 5});
    }
  });

  function returnToIndex() {
    window.location.href = 'le-index.html';
  }
</script>

</body>
</html>